package com.finger.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.finger.common.valid.AddGroup;
import com.finger.common.valid.UpdateGroup;
import com.finger.gulimall.product.entity.AttrEntity;
import com.finger.gulimall.product.service.CategoryService;
import com.finger.gulimall.product.vo.AttrGroupWithAttrsVO;
import com.finger.gulimall.product.vo.AttrRelationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.finger.gulimall.product.entity.AttrGroupEntity;
import com.finger.gulimall.product.service.AttrGroupService;
import com.finger.common.utils.PageUtils;
import com.finger.common.utils.R;


/**
 * 属性分组
 *
 * @author finger
 * @email 909128695@qq.com
 * @date 2022-03-16 13:38:29
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list/{cateLogId}")
    // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable Long cateLogId) {
        // PageUtils page = attrGroupService.queryPage(params);

        PageUtils page = attrGroupService.queryPage(params, cateLogId);

        return R.ok().put("page", page);
    }

    @RequestMapping("/{attrgroupId}/attr/relation")
    public R getGroupRelation(@PathVariable String attrgroupId) {

        // 根据分组Id查询该分组下的全部属性
        List<AttrEntity> groupRelation = attrGroupService.getGroupRelation(attrgroupId);

        return R.ok().put("data", groupRelation);
    }

    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrRelationVO> attrRelationVO) {
        // 保存属性和属性分组中间表关系
        attrGroupService.addRelation(attrRelationVO);
        return R.ok();
    }

    // /api/product/attrgroup/1/noattr/relation
    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R getNoGroupAttr(@PathVariable Long attrgroupId,
                            @RequestParam Map<String, Object> param) {

        // 获取当前分类下没有绑定分组的全部属性
        PageUtils page = attrGroupService.getNoGroupAttr(attrgroupId, param);
        return R.ok().put("page", page);
    }

    // /product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable Long catelogId) {

        // 根据分类id获取分类下的全部属性分组和属性分组下的属性
        List<AttrGroupWithAttrsVO> attrs = attrGroupService.getAttrGroupWithAttrs(catelogId);

        return R.ok().put("data", attrs);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    // @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {

        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        // 获取当前属性分组的全部菜单id
        Long[] paths = categoryService.getPaths(attrGroup.getCatelogId());

        attrGroup.setPaths(paths);
        return R.ok().put("attrGroup", attrGroup);

    }


    @PostMapping("/attr/relation/delete")
    public R delRelation(@RequestBody AttrRelationVO[] attrRelationVO) {

        // 删除属性和属性分组中间表中关联的数据，等于属性分组和属性解除关联
        attrGroupService.removeBatchAttrGroupRelation(attrRelationVO);
        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attrgroup:save")
    public R save(@Validated(AddGroup.class) @RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attrgroup:update")
    public R update(@Validated(UpdateGroup.class) @RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
